package com.cocotingo.snail.template.models;

import java.awt.Font;

import com.cocotingo.snail.template.TemplateSyntaxException;

public abstract class ResourceNode extends IDNode {
	
	public static final int IMAGE = 0;
	public static final int STRETCHABLE_IMAGE = 1;
	public static final int COLOR = 2;
	public static final int FONT = 3;
	
	public abstract int getType();
	
	@Override
	public Iterable<Node> children() {
		return Node.EMPTY_ITERABLE;
	}
	
	public static class ImageNode extends ResourceNode {

		private String resourceName;
		
		@Override
		public int getType() {
			return IMAGE;
		}
		
		public String getResourceName() {
			return resourceName;
		}
		
		public void setResourceName(String resourceName) {
			this.resourceName = resourceName;
		}
		
		@Override
		public void validateNode() throws TemplateSyntaxException {
			super.validateNode();
			if (resourceName == null || resourceName.isEmpty()) throw new TemplateSyntaxException(this, "ImageNode.resourceName shouldn't be empty/null.");
		}

		@Override
		protected String getXMLNodeName() {
			return "Image";
		}

		@Override
		protected String getXMLAttributes() {
			return String.format("id=\"%s\" resource=\"%s\"", getID(), resourceName);
		}
		
	}
	
	public static class StretchableImageNode extends ImageNode {
		
		private int x1, y1, x2, y2;
		
		@Override
		public int getType() {
			return STRETCHABLE_IMAGE;
		}
		
		public int getX1() {
			return x1;
		}
		
		public int getX2() {
			return x2;
		}
		
		public int getY1() {
			return y1;
		}
		
		public int getY2() {
			return y2;
		}
		
		public void setAnchors(int x1, int y1, int x2, int y2) {
			this.x1 = x1;
			this.y1 = y1;
			this.x2 = x2;
			this.y2 = y2;
		}
		
		@Override
		protected String getXMLNodeName() {
			return "StretchableImage";
		}

		@Override
		protected String getXMLAttributes() {
			return String.format("id=\"%s\" resource=\"%s\" anchors=\"%d,%d,%d,%d\"", getID(), getResourceName(), x1, y1, x2, y2);
		}
		
	}

	public static class ColorNode extends ResourceNode {
		
		private int color;
		
		public ColorNode() {
			color = 0xffffff;
		}
		
		public int getColor() {
			return color;
		}
		
		public void setColor(int color) {
			this.color = color;
		}

		@Override
		public int getType() {
			return COLOR;
		}

		@Override
		protected String getXMLNodeName() {
			return "Color";
		}

		@Override
		protected String getXMLAttributes() {
			return String.format("id=\"%s\" rgb=\"0x%h\"", getID(), color);
		}
		
	}

	public static class FontNode extends ResourceNode {
		
		private String fontName;
		private int fontSize, fontStyle;
		
		public FontNode() {
			fontName = Font.DIALOG;
			fontStyle = Font.PLAIN;
		}
		
		public String getFontName() {
			return fontName;
		}
		
		public void setFontName(String fontName) {
			this.fontName = fontName;
		}
		
		public int getFontSize() {
			return fontSize;
		}
		
		public void setFontSize(int fontSize) {
			this.fontSize = fontSize;
		}
		
		public int getFontStyle() {
			return fontStyle;
		}
		
		public void setFontStyle(int fontStyle) {
			this.fontStyle = fontStyle;
		}

		@Override
		public int getType() {
			return FONT;
		}
		
		@Override
		public void validateNode() throws TemplateSyntaxException {
			super.validateNode();
			if (fontName == null || fontName.isEmpty()) throw new TemplateSyntaxException(this, "FontNode.fontName shouldn't be empty/null.");
			if (fontSize <= 0) throw new TemplateSyntaxException(this, "FontNode.fontSize should be positive number.");
		}

		@Override
		protected String getXMLNodeName() {
			return "Font";
		}

		@Override
		protected String getXMLAttributes() {
			StringBuffer buf = new StringBuffer(String.format("id=\"%s\"", getID()));
			if (fontName != null) buf.append(String.format(" name=\"%s\"", fontName));
			if (fontStyle == Font.PLAIN) {
				buf.append(" style=\"plain\"");
			} else if (fontStyle == Font.BOLD) {
				buf.append(" style=\"bold\"");
			} else if (fontStyle == Font.ITALIC) {
				buf.append(" style=\"italic\"");
			} else if (fontStyle == Font.BOLD + Font.ITALIC) {
				buf.append(" style=\"bold+italic\"");
			}
			buf.append(String.format(" size=\"%d\"", fontSize));
			return buf.toString();
		}
	}
	
}
